package com.budgetme.ui.fragments;

import com.budgetme.R;
import com.budgetme.database.Database;

import android.app.Activity;
import android.app.ListFragment;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

// TODO: Probably put budget remaining and such on the top of this activity

public class ExpenseListFragment extends ListFragment {
	private static final String TAG = "activities.fragments.ExpenseListFragment";

	private Activity mParentActivity;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    	Log.d(TAG, "onCreateView");
    	
    	// Inflate the layout for this fragment
    	View fragment = inflater.inflate(R.layout.expense_list_fragment, container, false);
    	
    	// Get the parent activity
    	mParentActivity = getActivity();
    	
		updateListAdapter();
		
		return fragment;
    
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
//		mExpenseSelectedListener.onExpenseSelected((int) id); 
	}
	
	// TODO: This is the wrong way to do this
	public void updateListAdapter() {
		
		// Get enabled budgets
		
		// For each budget, get current bucket
		
		// for each bucket, get expenses
		
		
		Uri uri = Uri.withAppendedPath(Database.tbl_expenses.CONTENT_URI, "budget");
		String[] projection = new String[] { Database.tbl_expenses._id, Database.tbl_budgets.t_name, Database.tbl_expenses.e_date, Database.tbl_expenses.e_amount };
		Cursor cursor = mParentActivity.getContentResolver().query(uri, projection, null, null, null);
		
		String[] mColumns = new String[] {Database.tbl_expenses.e_date,  Database.tbl_budgets.t_name, Database.tbl_expenses.e_amount };
		int[] mViewIds = new int[] { R.id.expense_list_expense_date, R.id.expense_list_budget_name, R.id.expense_list_expense_amount };
		
		SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(mParentActivity, R.layout.expense_list_contents, cursor, mColumns, mViewIds, 0);
		
		setListAdapter(mAdapter);
		
	}
	
    @Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    	menu.clear();
	    inflater.inflate(R.menu.expense_list_fragment_menu, menu);
	    super.onCreateOptionsMenu(menu, inflater);
	}
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.expense_list_menu_add:
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.d(TAG, "onAttach");
    }
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");
		setHasOptionsMenu(true);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
	}
	
	@Override
	public void onPause() {
		super.onPause();
		Log.d(TAG, "onPause");
	}
	
	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume");
		updateListAdapter();
	}
	
// These are in case I need to communicate to a different class on a click (such as when creating a widget)
	
//	public interface OnAddExpenseSelectedListener {
//		public void onAddExpenseSelected();
//	}
//	
//	public interface OnExpenseSelectedListener {
//		public void onExpenseSelected(int id);
//	}
	
}





